package com.iexbuy.ihg.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.adapter.ViewPageAdapter;
import com.iexbuy.ihg.common.CommUtil;
import com.iexbuy.ihg.entity.UserObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.ui.activity.MainTabActivity;
import com.iexbuy.ihg.ui.activity.StartActivity;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.ToastUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ForgetPsswrodFragment extends BaseFragment {

	@ViewInject(R.id.userName)
	private EditText userName;
	@ViewInject(R.id.password)
	private EditText password;
	@ViewInject(R.id.mobileNo)
	private EditText mobileNo;
	@ViewInject(R.id.validateCode)
	private EditText validateCode;
	@ViewInject(R.id.regPassword)
	private EditText regPassword;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_forget_password, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View view) {
		mBaseActivity.setTitle(mBaseActivity.getResources().getString(R.string.login_forgetpassword));
	}

	@Override
	public void onResume() {
		super.onResume();
	}


	
	private void login(final String username, final String passwordMd5) {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("username", username);
		paramMap.put("pwd", passwordMd5);

		ServiceHelper.send(mBaseActivity, ServiceHelper.LOGIN, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						mBaseActivity.showDialog("登录中...");
					}

					@Override
					public void onCancelled() {
						mBaseActivity.closeDialog();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						mBaseActivity.closeDialog();
						ToastUtils.toast(mBaseActivity, "登录成功。");
						String responseString = responseInfo.result;
						Log.i("", responseString);
						UserObject userInfo = JsonUtils.parseJsonStrToObject(responseString, UserObject.class);
						App.sPreferences.setUserName(username);
						App.sPreferences.setName(userInfo.getName());
						App.sPreferences.setPsw(passwordMd5);
						App.sPreferences.setUserId(userInfo.getId());
						App.sPreferences.setLoginIn(true);

						startActivity(new Intent(mBaseActivity, MainTabActivity.class));

						mBaseActivity.finish();
						StartActivity.getInstance().finish();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						mBaseActivity.closeDialog();
						if (msg.contains("apache.http.conn")) {
							CommUtil.showAlert(mBaseActivity, "登录失败", "连接不上服务器", null);
						} else {
							CommUtil.showAlert(mBaseActivity, "登录失败", "用户名或密码不对", null);
						}

					}
				});
	}


	@OnClick(R.id.getValidateCodeBtn)
	public void onGetValidateCode(View view) {
		String mobile = mobileNo.getText().toString();
		if (TextUtils.isEmpty(mobile)) {
			ToastUtils.toast(mBaseActivity, "请输入手机号码。");
		} else {
			HashMap<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("mobileNo", mobile);

			ServiceHelper.send(mBaseActivity, ServiceHelper.GET_VERIFYCODE, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						mBaseActivity.showDialog("请求中...");
					}

					@Override
					public void onCancelled() {
						mBaseActivity.closeDialog();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						mBaseActivity.closeDialog();
						ToastUtils.toast(mBaseActivity, "验证码已发送，请留意手机短信。");
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						mBaseActivity.closeDialog();
						if (msg.contains("apache.http.conn")) {
							CommUtil.showAlert(mBaseActivity, "登录失败", "连接不上服务器", null);
						} else {
							CommUtil.showAlert(mBaseActivity, "登录失败", msg, null);
						}

					}
				});
		}
	}

	@OnClick(R.id.checkValidaCodeBtn)
	public void onGheckValidaCodeBtn(View view) {
		String valCode = validateCode.getText().toString();
		final String mobile = mobileNo.getText().toString();
		final String password = regPassword.getText().toString();
		if (TextUtils.isEmpty(mobile)) {
			ToastUtils.toast(mBaseActivity, "请输入手机号码。");
			return;
		}
		if (TextUtils.isEmpty(valCode)) {
			ToastUtils.toast(mBaseActivity, "请输入验证码。");
			return;
		}
		if (TextUtils.isEmpty(password)) {
			ToastUtils.toast(mBaseActivity, "请输入密码。");
			return;
		}

		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("mobileNo", mobile);
		paramMap.put("verifyCode", valCode);

		ServiceHelper.send(mBaseActivity, ServiceHelper.CHECK_VERIFY_CODE, paramMap,
			new RequestCallBack<String>() {
				@Override
				public void onStart() {
					mBaseActivity.showDialog("请求中...");
				}

				@Override
				public void onCancelled() {
					mBaseActivity.closeDialog();
				}

				@Override
				public void onSuccess(ResponseInfo<String> responseInfo) {
					mBaseActivity.closeDialog();

					sendPassword(mobile, password);
				}

				@Override
				public void onFailure(HttpException error, String msg) {
					mBaseActivity.closeDialog();
					if (msg.contains("apache.http.conn")) {
						CommUtil.showAlert(mBaseActivity, "登录失败", "连接不上服务器", null);
					} else {
						CommUtil.showAlert(mBaseActivity, "登录失败", msg, null);
					}

				}
			});
	}

	private void sendPassword(final String mobile, final String password) {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("mobileNo", mobile);
		paramMap.put("password", password);

		ServiceHelper.send(mBaseActivity, ServiceHelper.REGISTER_WITH_PASSWORD, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						mBaseActivity.showDialog("请求中...");
					}

					@Override
					public void onCancelled() {
						mBaseActivity.closeDialog();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						mBaseActivity.closeDialog();

						ToastUtils.toast(mBaseActivity, "恭喜！密码修改成功！请用新密码登录！");

						login(mobile, password);
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						mBaseActivity.closeDialog();
						if (msg.contains("apache.http.conn")) {
							CommUtil.showAlert(mBaseActivity, "请求失败", "连接不上服务器", null);
						} else {
							CommUtil.showAlert(mBaseActivity, "请求失败", msg, null);
						}

					}
				});

	}

}
